组件传值

介绍

  • 父给子传值,可以用const的方式,不一定要class XXX extends Component
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
//父
<CityPickerWrapper onGetCityPickerLable={this.getCityPickerLable.bind(this)} onGetMoveEndData={this.state.moveEndData}/>
//子
<Picker extra = {this.props.onGetMoveEndData}>
<CitySelected anotherExtra={this.props.onGetMoveEndData}/>
</Picker>
//子子
const CitySelected = props => {
//some code
let extraData = props.anotherExtra.split(',')
return(
<div className="city-selected-wrap" onClick={props.onClick}>
<div className="city-selected-div">{extraData[2]}</div>
</div>
)
}

ps:注意props.children

  • 子给父传值,上代码:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
/**
*Sample React Native App
。    ∧_∧。゚
 ゚  (゚ ´Д`゚)っ゚
   (つ  /
    | (⌒)
    し⌒
*@Anthony
*/
import React,{Component} from 'react';
import{
View,
TouchableOpacity,
Text,
StyleSheet,
Dimensions,
ListView,
Image,
} from 'react-native';
//引用头部
import Header from '../common/commonHeader';
import SubTabContent from './subTabContent';
//获取屏幕宽高
const {height} = Dimensions.get('window');
//获取json数据
var SubTabView = require('./studyView.json');
var SubTabViewData = SubTabView.lession;
var ItemArrLeft = [];
var ItemArrRight = [];
for(var i in SubTabViewData){
ItemArrLeft.push(
SubTabViewData[i].title
);
ItemArrRight.push(
SubTabViewData[i].neirong
);
}
//整个选项视图
class SubTab extends Component{
constructor(props){
super(props);
this.state = {
val:null,
};
}
_getSubView(val){
this.setState({
val : val,
})
const { navigator } = this.props;
if(navigator) {
navigator.push({
name: 'SubTabContent',
component: SubTabContent,
params:{
val:val,
}
})
}
}
render(){
return(
<View style={styles.container}>
<Header title={'爸爸妈妈上课了'} />
<Tab navs={ItemArrLeft} contents={ItemArrRight} onSelect={this._getSubView.bind(this)} />
</View>
);
}
}
//自定义的选项Tab
var Tab = React.createClass({
getInitialState : function() {
return {
index : 0,
};
},
handleClick : function(index) {
this.setState({
index : index,
})
},
render : function() {
return (
<View style={{flex: 1,flexDirection:'row'}}>
<NavClass clk={this.handleClick} index={this.state.index} navs={this.props.navs} />
<ContentsClass index={this.state.index} contents={this.props.contents} onSelect={this.props.onSelect} />
</View>
)
}
});
//Tab左边侧栏目
class NavClass extends Component{
render(){
return(
<View style={styles.subTab}>
{this.props.navs.map(
(ele,index)=>{
let statedBg = {
backgroundColor: this.props.index == index ? '#fafafa' : 'transparent',
zIndex:1
}
return(<TouchableOpacity activeOpacity={0.8} style={[styles.button,statedBg]} onPress={this.props.clk.bind(null,index)} key={index}><Text>{ele}</Text></TouchableOpacity>)
}
)
}
<View style={styles.line}></View>
</View>
);
}
};
//Tab右边侧栏目
class ContentsClass extends Component{
constructor(props){
super(props);
const ds = new ListView.DataSource({rowHasChanged:(r1,r2)=>r1 !== r2});
this.state = {
dataSource:ds,
data:this.props.contents
};
}
renderSubListWrap(rowData,sectionId,rowId){
let specialLenth = this.props.contents.length-1;
let tagBg = {
backgroundColor:this.props.index == specialLenth ? 'red' : 'transparent',
}
let tagCol = {
color:this.props.index == specialLenth ? '#fff' : 'transparent',
}
let specialDataLenth = this.state.data[specialLenth].length;
let rowIdNew = Math.abs(rowId - specialDataLenth);
return(
<TouchableOpacity activeOpacity={0.8} style={styles.subListWrap} onPress={()=>{this.props.onSelect(rowData)}}>
<Image style={styles.subListImg} resizeMode={Image.resizeMode.stretch} source={{uri: rowData.thumb}}/>
<View style={styles.textView}>
<Text>{rowData.title}</Text>
</View>
<View style={[styles.tagNum,tagBg]}>
<Text style={tagCol}>{rowIdNew}</Text>
</View>
</TouchableOpacity>
)
}
render(){
let num = this.props.index;
return(
<View style={styles.subList}>
<ListView
dataSource={this.state.dataSource.cloneWithRows(this.state.data[num])}
renderRow={this.renderSubListWrap.bind(this)}
showsVerticalScrollIndicator={false}
/>
</View>
)
}
};
//View样式
const styles = StyleSheet.create({
container:{
flex:1,
},
/*左侧按钮*/
subTab:{
flex: 1,
backgroundColor:'#def4ff',
},
button:{
height:50,
borderBottomWidth:2,
borderColor:'#dce3e9',
justifyContent:'center',
alignItems:'center',
},
line:{
width:2,
height,
backgroundColor:'#dce3e9',
position:'absolute',
top:0,
right:0,
zIndex:0
},
/*右侧数据*/
subList:{
flex: 2,
},
subListWrap:{
marginLeft:10,
marginRight:10,
marginTop:10,
},
subListImg:{
height:80,
},
textView:{
flexDirection:'row',
justifyContent:'center',
alignItems:'flex-end',
height:25,
},
tagNum:{
flexDirection:'row',
justifyContent:'center',
alignItems:'center',
position:'absolute',
top:0,
right:0,
width:15,
height:15,
}
})
export default SubTab;

#139行 onPress={()=>{this.props.onSelect(rowData)}} 传给父级的onSelect

#91行 onSelect={this.props.onSelect} 传的父级的onSelect

#70行 onSelect={this._getSubView.bind(this)}

#51行 _getSubView的val就是rowData

[父组件不知道rowdata] 这样通过子组件传过来

#56行 通过navigator传给第二级页面,二级页面可以获取传的值,这里是标题title

未测

下面这个方法是偶然看见的,尚未测试。